#pragma once
#include "tstring.h"
#define VALID_RUN
#ifdef VALID_RUN
#ifdef _DEBUG
	#define VALID(X) _ASSERT(X)
#else
#define VALID(X) if (!(X)) LogError(tstring(_T(#X)));
#endif //_DEBUG
#else
	#define VALID(X) 0
#endif //VALID_RUN


template<int v>
struct Int2Type
{
	enum {value = v};
};

template <typename T>
struct Type2Type
{
	typedef T type;
};

template <typename T>
static void SafeDelete(T*& t)
{
	if (t)
	{
		delete t;
		t = 0;
	}
}
template <class Index, class Type>
static void SafeDeleteAll(std::map<Index, Type*>& t)
{
	for (typename std::map<Index, Type*>::iterator it(t.begin());
		it != t.end(); ++it)
	{
		if(it->second)
		{
			delete it->second;
			it->second = 0;
		}
	}
	t.clear();
}

template <class Type>
static void SafeDeleteAll(std::vector<Type*>& t)
{
	for (typename std::vector<Type*>::iterator it(t.begin());
		it != t.end(); ++it)
	{
		if(*it)
		{
			delete *it;
			*it = 0;
		}
	}
	t.clear();
}

template <class Type>
static void SafeDeleteAll(std::list<Type*>& t)
{
	for (typename std::list<Type*>::iterator it(t.begin());
		it != t.end(); ++it)
	{
		if(*it)
		{
			delete *it;
			*it = 0;
		}
	}
	t.clear();
}

template <typename T>
static void SafeDrop(T*& t)
{
	if (t)
	{
		t->drop();
		t = 0;
	}
}

template <class Type>
static void SafeDropAll(std::vector<Type*>& t)
{
	for (typename std::vector<Type*>::iterator it(t.begin());
		it != t.end(); ++it)
	{
		if(*it)
		{
			(*it)->drop();
			*it = 0;
		}
	}
	t.clear();
}

static std::wstring ToWString(const std::string& s)
{
	std::wstring r;
	const size_t size = 2048;
	wchar_t tmp[size];
	size_t length = s.length();
	while (length > 0)
	{
		size_t cl = size;
		if (length < size)
		{
			cl = length;
		}
		size_t num;
		mbstowcs_s(&num, tmp, s.c_str(), cl);
		r.append(tmp);
		length -= cl;
	}
	
	return r;
}

#define FOR_EACH_T(LISTTYPE,VAR,LIST) \
	for (typename LISTTYPE::iterator VAR = (LIST).begin(),last = (LIST).end();VAR != last;VAR++)
#define FOR_EACH_CONST_T(LISTTYPE,VAR,LIST) \
	for (typename LISTTYPE::const_iterator VAR = (LIST).begin(),last = (LIST).end();VAR != last;VAR++)

#define FOR_EACH(LISTTYPE,VAR,LIST) \
	for (LISTTYPE::iterator VAR = (LIST).begin(),last = (LIST).end();VAR != last;VAR++)
#define FOR_EACH_CONST(LISTTYPE,VAR,LIST) \
	for (LISTTYPE::const_iterator VAR = (LIST).begin(),last = (LIST).end();VAR != last;VAR++)


#define CASE_OnSEvent(X) case X::TypeValue: OnSEvent(static_cast<X&>(protocol)); break;
#define CASE_OnCEvent(X) case X::TypeValue: OnCEvent(static_cast<X&>(protocol)); break;
#define CASE_RetString(X) case X: return _T(#X);
#define CASE_RetWString(X) case X: return L#X;


